package dao;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import pojo.Chitiethoadon;
import pojo.ChitiethoadonId;
import pojo.Hinhthucthanhtoan;
import pojo.Hoadon;
import pojo.Sanpham;
import pojo.Taikhoan;
import util.ConvertHelper;
import util.HibernateUtil;

public class HoadonDAO extends AbstractDAO<Hoadon, Integer> {

	@Override
	protected Class getPOJOClass() {
		// TODO Auto-generated method stub
		return Hoadon.class;
	}

	@Override
	protected Query MakeHQL() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Hoadon> GetHoaDon(int maTaiKhoan) {
		String hql = "select hd from Hoadon hd where hd.taikhoan.mataikhoan=" + maTaiKhoan;
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query selectClause = session.createQuery(hql);

		List<Hoadon> dsHoaDon = selectClause.list();

		//System.out.println(ds.get(0).toString());

		return dsHoaDon;
	}
	
	public int GetMaHoaDonLonNhat() {
		String hql = "select max(mahoadon) from Hoadon";

		Session session = HibernateUtil.getSessionFactory().openSession();
		Query selectClause = session.createQuery(hql);

		List<Integer> ds = selectClause.list();

		System.out.println(ds.get(0).toString());

		return ds.get(0);
	}

	public int ThemHoaDonMoi(HttpServletRequest request, HttpSession session) {

		ArrayList<Sanpham> ds = (ArrayList<Sanpham>) session
				.getAttribute("gioHang");

		HoadonDAO dao = new HoadonDAO();
		// Hien tai = Max + 1
		int maHoaDonHienTai = dao.GetMaHoaDonLonNhat() + 1;

		// Tạo Hóa đơn
		String s = request.getParameter("ptThanhToan");
		int htttIndex = Integer.valueOf(s);

		Hinhthucthanhtoan httt = (new HinhthucthanhtoanDAO()).getList().get(
				htttIndex);
		Taikhoan tk = (new TaikhoanDAO()).get(((Taikhoan) (session
				.getAttribute("taiKhoan"))).getMataikhoan());
		String diachinhanhang = ConvertHelper.GetRequestUnicodeString(request,
				"diaChiNguoiNhan");

		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		System.out.println(dateFormat.format(date));

		// Hoadon hoadon = dao.get(maHoaDonHienTai);
		Hoadon hd = new Hoadon();
		hd.setMahoadon(maHoaDonHienTai);
		hd.setHinhthucthanhtoan(httt);
		hd.setTaikhoan(tk);
		hd.setDiachinhanhang(diachinhanhang);
		hd.setNgayxuat(date);
		hd.setDaduyet(false);
		
		// Lưu hóa đơn
		// ----------------------
		Session hiberSession = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = hiberSession.beginTransaction();

		hiberSession.save(hd);
		tx.commit();
		System.out.println("Done HoaDon");
		// ---------------------

		// Tạo chi tiết hóa đơn
		Chitiethoadon cthd = null;
		ChitiethoadonId cthdid = null;
		Sanpham sp = null;

		tx = hiberSession.beginTransaction();
		for (int i = 0; i < ds.size(); i++) {

			sp = (Sanpham) ds.get(i);

			cthdid = new ChitiethoadonId();
			cthdid.setMahoadon(maHoaDonHienTai);
			cthdid.setMasp(sp.getMasanpham());

			cthd = new Chitiethoadon();
			cthd.setDongia((sp.getGiaban()) / 1000);
			cthd.setHoadon(hd);
			cthd.setId(cthdid);
			cthd.setSanpham(sp);
			cthd.setSoluong(sp.getSoluongtronggiohang());

			// hiberSession.save(cthdid);
			hiberSession.save(cthd);

			sp.setSoluongban(sp.getSoluongban() + sp.getSoluongtronggiohang());
			hiberSession.update(sp);
		}
		tx.commit();

		return maHoaDonHienTai;
	}
}
